﻿#include "getserverfromdb.h"
#include "ui_getserverfromdb.h"
#include "common.h"
#include <QMessageBox>
#include <QSqlQueryModel>
#include <QTableView>
#include <QDebug>
#include <QSqlError>
#include "logger.h"
#include "dbservice.h"
#include "computermanager.h"


#define DEFAULT_PORT_FROM_DB "3306"
#define DEFAULT_USERNAME_FROM_DB "dev"
#define DEFAULT_DBNAME_FROM_DB "face"

GETSERVERFROMDB::GETSERVERFROMDB(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::GETSERVERFROMDB)
{
    ui->setupUi(this);
    this->setMaximumSize(401,301);
    this->setMinimumSize(401,301);

    ui->passwordLineEdit->setEchoMode(QLineEdit::Password);
    ui->portLineEdite->setPlaceholderText(DEFAULT_PORT_FROM_DB);
    ui->usernameLineEdit->setPlaceholderText(DEFAULT_USERNAME_FROM_DB);
    ui->DBLineEdit->setPlaceholderText(DEFAULT_DBNAME_FROM_DB);

}

GETSERVERFROMDB::~GETSERVERFROMDB()
{
    delete ui;
}

void GETSERVERFROMDB::on_cancelButton_clicked(bool checked)
{
    this->close();
}

void GETSERVERFROMDB::on_getButton_clicked(bool checked)
{
    int successCount = 0;
    int failCount = 0;
    QString ip = ui->IPLineEdite->text();
    QString port = ui->portLineEdite->text().length() == 0?DEFAULT_PORT_FROM_DB:ui->portLineEdite->text();
    QString userName = ui->usernameLineEdit->text().length() == 0?DEFAULT_USERNAME_FROM_DB:ui->usernameLineEdit->text();
    QString password = ui->passwordLineEdit->text();
    QString dbName = ui->DBLineEdit->text().length() == 0?DEFAULT_DBNAME_FROM_DB:ui->DBLineEdit->text();

    if(ip.length() == 0){
        QMessageBox::information(this,"查询条件错误！","IP地址不能为空！");
    }

    QSqlDatabase mysqlDB;
    if(connectDB(ip,port.toInt(),dbName,userName,password,mysqlDB)){
        QSqlQuery sql_query(mysqlDB);
        QString select_sql = "SELECT name,s_ip FROM v_servers";
        if(!sql_query.exec(select_sql))
        {
            qDebug()<<sql_query.lastError();
        }
        else
        {
            while(sql_query.next())
            {
                QString name = sql_query.value(0).toString();
                QString s_ip = sql_query.value(1).toString();
                qDebug()<<QString("s_ip:%1    name:%2").arg(s_ip).arg(name);
                Computer temp;
                if(gComputerManager.getOneComputerByIP(s_ip.toStdString(), temp) == f_fail) //not found
                {
                    saveComputer(s_ip,name,ip,dbName);
                    successCount++;
                }else{
                    failCount++;
                }
            }
        }

        mysqlDB.close();
    }


    this->close();
    QMessageBox::information(this,"提示", "共"+int2QStr(successCount+failCount)+"条记录，"+"成功"+int2QStr(successCount)+"，重复"+int2QStr(failCount));
}


bool GETSERVERFROMDB::connectDB(QString ip,int port,QString DBName,QString userName,QString password,QSqlDatabase &mysqlDB)
{
    mysqlDB = QSqlDatabase::addDatabase("QMYSQL","qt_mysql_connection_"+ip);
    mysqlDB.setHostName(ip);
    mysqlDB.setPort(port);
    mysqlDB.setDatabaseName(DBName);
    mysqlDB.setUserName(userName);
    mysqlDB.setPassword(password);
    return mysqlDB.open();
}

void GETSERVERFROMDB::saveComputer(QString &ip,QString &name,QString &server,QString &dbName)
{
    Computer computer;
    computer.name =name;
    computer.ip = ip;
    computer.fromSource = ComputerFromSource_importFromDB;
    computer.comment = "来自数据库："+server+",库名:"+dbName;

    if(!f_success == gComputerManager.saveOneComputer(computer))
    {
        QMessageBox::information(this,"提示", "保存服务器失败");
    }


}
